//获取功能菜单列表
$('.roleNameAuthority').click(function(){
	//先获取自定义标签属性
	var authority =$(this);
	var roleId=authority.attr("roleid");
	var rolename=authority.attr("rolename");
	$("#selectrole").html("当前配置角色为："+rolename);
	//给隐藏域roleidhide 赋值
	$("#roleidhide").val(roleId);
	//获取功能列表
	$.ajax({
		url:'/backend/functions.html',
		type:'POST',
		data:{fid:roleId},
		dataType:'json',
		timeout:1000,
		error:function(){
			alert("加载功能列表失败！");
		},
		success: function(result){
			if (result=="nodata") {
				alert("对不起，功能列表获取失败，请重试！")
			}else{
				//显示功能列表
				listr="";
				for (var i = 0; i <result.length; i++) {
					//主功能菜单
					listr+="<li>";
					listr += "<ul id=\"subfuncul"+result[i].mainFunction.id+"\" class=\"subfuncul\">";
					listr += "<li  class=\"functiontitle\" ><input id='functiontitle"+result[i].mainFunction.id+"' onchange='mainFunctionSelectChange(this,"+result[i].mainFunction.id+");' funcid=\""+result[i].mainFunction.id+"\" type='checkbox' />"+result[i].mainFunction.functionName+"</li>";
					for (var j = 0; j < result[i].subFunctions.length; j++) {
						//循环生成子功能菜单
						listr += "<li><input onchange='subFunctionSelectChange(this,"+result[i].mainFunction.id+");' funcid=\""+result[i].subFunctions[j].id+"\" type='checkbox' /> "+result[i].subFunctions[j].functionName+"</li>";
					}
					listr += "</ul></li>";
				}	
				$("#functionList").html(listr);
				
				//checkbox框的回显操作
				$("#functionList :checkbox").each(function(){
					var checkbox=$(this);
					var fid=checkbox.attr("funcid");
					var rid=$("#roleidhide").val();
					$.ajax({
						url:'/backend/getAuthorityDefault.html',
						type:'POST',
						data:{rid:rid,fid:fid},
						dataType:'html',
						timeout:1000,
						error:function(){
							alert("回显勾选失败！");
						},
						success:function(result){
							if ("success"==result) {
								checkbox.attr("checked",true); 
							}else{
								checkbox.attr("checked",false); 
							}
						}
					});
				});
				
					
			}
		}
	});
})

function subFunctionSelectChange(obj,id){
	if(obj.checked){
		$("#functiontitle"+id).attr("checked", true);  
	}
}

function mainFunctionSelectChange(obj,id){
	if(obj.checked){
		$("#subfuncul"+id+" :checkbox").attr("checked", true);  
	}else{
		$("#subfuncul"+id+" :checkbox").attr("checked", false);  
	}	
	//alert($(this) +　id);
}

$("#selectAll").click(function () {//全选  
    $("#functionList :checkbox").attr("checked", true);  
});  

$("#unSelect").click(function () {//全不选  
    $("#functionList :checkbox").attr("checked", false);  
});  

$("#reverse").click(function () {//反选  
    $("#functionList :checkbox").each(function () {  
        $(this).attr("checked", !$(this).attr("checked"));  
    });  
});  


//提交按钮
$("#confirmsave").click(function() {
	var a="您要确定修改当前角色的权限么？";
	if (confirm(a)) {
		//roleId functionIds 全部被选中的functionid
		//拼接ids  将ids传到后台
		var ids=$("#roleidhide").val()+"-";
		$("#functionList :checkbox").each(function(){
			//循环将checkbox中选中的放入ids中
			if($(this).attr("checked")=='checked'){
				ids+=$(this).attr("funcid")+"-";
			}
		});
		//ajax提交
		$.ajax({
			url:'/backend/modifyAuthority.html',
			type:'POST',
			data:{ids:ids},
			dataType:'html',
			timeout:1000,
			error:function(){
				alert("修改权限失败！");
			},
			success:function(result){
				if(result=="nodata"){
					alert("对不起功能列表获取失败");
				}else{
					alert("恭喜你，权限修改成功！");
				}
			}
		});		
	}
});













